package com.qf.mybatisplus.controller;

import com.qf.mybatisplus.bean.entity.Student;
import com.qf.mybatisplus.bean.entity.User;
import com.qf.mybatisplus.bean.vo.TestVo;
import com.qf.mybatisplus.resultBean.JsonResult;
import com.qf.mybatisplus.service.StudentService;
import com.qf.mybatisplus.service.UserService;
import com.qf.mybatisplus.utils.RedisUtils;
import io.swagger.annotations.*;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Api(value = "测试接口", tags = "测试接口")
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private UserService userService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private RedisUtils redisUtils;


    @ApiOperation(value = "测试方法1")
    @PostMapping("/test1")
    public JsonResult test1() {
        Student student = studentService.getOne("小王");
        System.err.println(student);
        return JsonResult.resultOk(student);
    }


    @PostMapping("/test2")
    @ApiOperation(value = "测试参数校验以及Vo转实体类", notes = "", response = JsonResult.class)
    public JsonResult testCheck(@Validated TestVo testVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String errorMsg = bindingResult.getFieldError().getDefaultMessage();
            return JsonResult.failed(errorMsg);
        }
        List<User> userList = userService.getUserList(testVo);
        return JsonResult.resultOk(userList);
    }

    /**
     * 前端传入参数:
     * {
     * "name":"小王",
     * "teacher":{
     * "math":"张老师",
     * "eng":"李老师",
     * "Chinese":"王老师"
     * }
     * <p>
     * }
     **/
    // 用JSONObject 接收参数
    @ApiOperation(value = "用JSONObject接收json格式参数")
    @PostMapping(value = "/testJSONObject", produces = "application/json;charset=UTF-8")
    public JsonResult testJSONObject(@RequestBody @ApiParam(name = "提交填写的信息入参", value = "请求的json数据")
                                             JSONObject jsonObject) {

        return JsonResult.resultOk(jsonObject);
    }

    // 用Vo接收json参数
    @ApiOperation(value = "用Vo接收@RequestBody，接收json格式参数")
    @PostMapping("/testJSONObjectUserVo")
    public JsonResult testJSONObjectUserVo(@RequestBody @ApiParam(name = "提交填写的信息入参", value = "请求的json数据")
                                                   TestVo testVo) {

        return JsonResult.resultOk(testVo);
    }

    // 用Vo接收json参数
    @ApiOperation(value = "用Vo和文件接收@RequestBody，接收json格式参数和文件格式")
    @PostMapping("/testJSONObjectUserVoAndFile")
    public JsonResult testJSONObjectUserVoAndFile(
            @RequestBody @ApiParam(name = "提交填写的信息入参", value = "请求的json数据")
             TestVo testVo,
            @ApiParam(name = "file", value = "file", required = true)
             MultipartFile file
    ) {
        return JsonResult.resultOk(testVo);
    }


    // 用map接收json参数
    @ApiOperation(value = "用map接收json参数")
    @PostMapping("/testJSONObjectUseMap")
    public JsonResult testJSONObjectUseMap(@RequestBody @ApiParam(name = "提交填写的信息入参", value = "请求的json数据")
                                                   Map<String, Object> map) {

//        String name = (String) map.get("name");
//        List<TestVo.Person> friend = (List<TestVo.Person>) map.get("friend");
//        System.err.println(jsonObject);
        return JsonResult.resultOk(map);
    }


    // 用testRequestParam接收参数
    @ApiOperation(value = "直接接收formData格式参数,表单形式")
    @PostMapping(value = "/testRequestParam", headers = "content-type=multipart/form-data")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", paramType = "query"),
            @ApiImplicitParam(name = "file", value = "身份证正面照片", required = true, dataType = "String", paramType = "query"),
    })
    public JsonResult testRequestParam(
            @RequestParam(name = "id") Integer id,
            @ApiParam(name = "file", value = "file", required = true) MultipartFile file) {

        return JsonResult.resultOk();
    }


    // 用testRequestParam接收参数
    @ApiOperation(value = "redis加锁")
    @PostMapping("/testRedisLock")
    public JsonResult testRedisLock(String bfcqzh) {

        for (int i = 0; i < 10; i++) {
            redisUtils.set("zs:" + i, "加锁", 100L, TimeUnit.SECONDS);

        }


        String o = (String) redisUtils.get("zs:2");


        System.err.println(o);
        return JsonResult.resultOk();
    }

    // 用testRequestParam接收参数
    @ApiOperation(value = "testRequest")
    @PostMapping("/testRequest")
    public JsonResult testRequest(@RequestParam String name,@RequestParam Integer age) {

        System.err.println("name:"+name);
        System.err.println("age:"+age);
        return JsonResult.resultOk();
    }


    // 用testRequestParam接收参数
    @ApiOperation(value = "paramByJson")
    @PostMapping("/paramByJson")
    public JsonResult paramByJson(@RequestBody User user) {

        System.err.println("name:"+user.getName());
        System.err.println("age:"+user.getAge());
        return JsonResult.resultOk();
    }

    // 用testRequestParam接收参数
    @ApiOperation(value = "apiTest")
    @GetMapping("/apiTest")
    public JsonResult apiTest(Integer id) {

        System.err.println("id:"+id);
        return JsonResult.resultOk(id);
    }



}
